package com.threeti.jiayusms.domain.service.defaulttemplate;

import java.util.List;

import static org.apache.commons.lang.StringUtils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.threeti.jiayusms.domain.model.defaulttemplate.DefaultCommunicationTemplate;
import com.threeti.jiayusms.domain.model.defaulttemplate.SMSTemplateMapping;
import com.threeti.jiayusms.domain.model.defaulttemplate.VoiceTemplateMapping;
import com.threeti.jiayusms.domain.service.legacy.JiaYuDBLegacyUpdateService;
import com.threeti.jiayusms.domain.util.TemplateIdGenerator;
import com.threeti.jiayusms.infrastructure.defaulttemplate.DefaultCommunicationTemplateDao;
import com.threeti.jiayusms.infrastructure.defaulttemplate.SMSTemplateMappingDao;
import com.threeti.jiayusms.infrastructure.defaulttemplate.VoiceTemplateMappingDao;

@Service
@Transactional
public class DefaultCommunicationTemplateUpdateServiceImpl implements DefaultCommunicationTemplateUpdateService {

  @Autowired
  private DefaultCommunicationTemplateDao dao;
  
  @Autowired  
  private SMSTemplateMappingDao smsTemplateDao;
  
  @Autowired  
  private VoiceTemplateMappingDao voiceTemplateDao;
  
  @Autowired  
  private JiaYuDBLegacyUpdateService legacyUpdateService;

  @Override
  public void removeDefaultTemplateBy(Long defaultTemplateId) {
    dao.removeBy(defaultTemplateId);
  }

  @Override
  public void removeDefaultTemplateAndLegacyDBAlsoBy(Long defaultTemplateId) {
    DefaultCommunicationTemplate defaultTemplate = dao.loadBy(defaultTemplateId);
    
    if (defaultTemplate != null) {
      legacyUpdateService.removeDefaultTemplateBy(defaultTemplate.getCustomId());
      
      dao.remove(defaultTemplate);
    }
  }

  @Override
  public void removeSubMsgTemplateOfDefaultTemplateBy(Long subMsgTemplateId) {
    SMSTemplateMapping smsMapping = smsTemplateDao.loadBy(subMsgTemplateId);
    
    if (smsMapping != null) {      
      legacyUpdateService.removeSMSMappingBy(smsMapping.getCustomId());
      
      smsTemplateDao.remove(smsMapping);
    }
  }

  @Override
  public void removeSubVoiceTemplateOfDefaultTemplateBy(Long subVoiceTemplateId) {
    VoiceTemplateMapping voiceMapping = voiceTemplateDao.loadBy(subVoiceTemplateId);
    
    if (voiceMapping != null) {      
      legacyUpdateService.removeVoiceMappingBy(voiceMapping.getCustomId());
      
      voiceTemplateDao.remove(voiceMapping);
    }
  }

  @Override
  public DefaultCommunicationTemplate storeDefaultTemplate(DefaultCommunicationTemplate defaultTemplate) {
    return  dao.store(defaultTemplate);
  }

  @Override
  public DefaultCommunicationTemplate storeDefaultTemplateAndtoLegacyDBAlso(DefaultCommunicationTemplate defaultTemplate, String smsorvoicekey) {
    //预存入
    DefaultCommunicationTemplate rtnDefaultTemplate = storeDefaultTemplate(defaultTemplate);
    

    //计算CustomId并持久到遗留数据库->
    String templateId = TemplateIdGenerator.generateTempalteIdBy("Model", rtnDefaultTemplate);
    rtnDefaultTemplate.setCustomId(templateId);
    
    if("sms".equals(smsorvoicekey)) {
      List<SMSTemplateMapping> smsMappings = rtnDefaultTemplate.getSmsTemplateMappings();
      if (smsMappings != null) {
        SMSTemplateMapping lastMapping = smsMappings.get(smsMappings.size()-1);
        if (lastMapping != null) {
          lastMapping.setCustomId(TemplateIdGenerator.generateTempalteIdBy("FS-SMS", lastMapping));

          legacyUpdateService.storeSMSMapping(rtnDefaultTemplate, lastMapping);
        }
      }
    } else {
      List<VoiceTemplateMapping> voiceMappings = rtnDefaultTemplate.getVoiceTemplateMappings();
      if (voiceMappings != null) {
        VoiceTemplateMapping lastMapping = voiceMappings.get(voiceMappings.size()-1);
        if (lastMapping != null) {
          lastMapping.setCustomId(TemplateIdGenerator.generateTempalteIdBy("FS-Voice", lastMapping));

          legacyUpdateService.storeVoiceMapping(rtnDefaultTemplate, lastMapping);
        }
      }
    }
    //<-
    return rtnDefaultTemplate;
  }

  @Override
  public void enableDefaultTemplateBy(Long defaultTemplateId) {
    DefaultCommunicationTemplate currentEnabledTemplate = dao.findDefaultEnabledTemplate();
    if (currentEnabledTemplate != null) currentEnabledTemplate.disable();
    
    DefaultCommunicationTemplate targetTemplate = dao.loadBy(defaultTemplateId);
    if (targetTemplate != null) {
      targetTemplate.enable();
      
      if (isNotEmpty(targetTemplate.getCustomId())) {
        legacyUpdateService.enableDefaultTemplateBy(targetTemplate.getCustomId());
      }
    }
  }

}
